const router = require('koa-router')()
const mongoose = require('mongoose')
const China = mongoose.model('China')


router.get('/', async (ctx, next) => {
    let data
    let query = ctx.query
    let keys = Object.keys(query)[0]
    let reg = new RegExp(`.*${query[keys]}.*`, "gi")

    if (keys == 'name') {
        await China.find({ 'name': reg }, function(err, docs) {
            if (err) {
                console.log(err)
                return
            } else {
                data = docs
            }
        })
    } else {
        await China.find({ 'cx16': reg }, function(err, docs) {
            if (err) {
                console.log(err)
                return
            } else {
                data = docs
            }
        })
    }
    ctx.body = data

    await next()
})



module.exports = router